﻿using System;
using Fuse8.ContentManagementFramework.SystemFacade;
using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.ValidationService;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.BizFacade.Validation;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.ContentManagementFramework.BizFacade
{
    public static class BusinessFacadePolicy
    {
        public static void Register()
        {
            RegisterBusinessServices();

            RegisterEntityRevisionServices();

            RegisterValidationServices();

            RegisterSearchServices();
        }

        private static void RegisterBusinessServices()
        {
            var container = DependencyServiceLocator.Current;

            //CmsSiteAdminMap            
            container.Register<IEntityBusinessService<CmsSiteAdminMap, int>>(new CmsSiteAdminMapService());

            //CmsDashboard
            container.Register<IEntityBusinessService<CmsDashboard, int>>(new CmsDashboardService());

            //CmsUserInformation
            container.Register<IEntityBusinessService<CmsUserInformation, int>>(new CmsUserInformationService());

            //CmsUserGroup
            container.Register<IEntityBusinessService<CmsUserGroup, int>>(new CmsUserGroupService());

            //CmsUser
            container.Register<IEntityBusinessService<CmsUser, int>>(new CmsUserService());

            //CmsMedia
            container.Register<IEntityBusinessService<CmsMedia, int>>(new CmsMediaService());

            //CmsContentType
            container.Register<IEntityBusinessService<CmsContentType, int>>(new CmsContentTypeService());

            //CmsContent
            container.Register<IEntityBusinessService<CmsContent, int>>(new CmsContentService());

            //CmsTemplate
            container.Register<IEntityBusinessService<CmsTemplate, int>>(new CmsTemplateService());

            //CmsPage
            container.Register<IEntityBusinessService<CmsPage, int>>(new CmsPageService());

            //CmsPageControl
            container.Register<IEntityBusinessService<CmsPageControl, int>>(new CmsPageControlService());

            //CmsModule
            container.Register<IEntityBusinessService<CmsModule, int>>(new CmsModuleService());

            //CmsWorkflow
            container.Register<IEntityBusinessService<CmsWorkflow, Guid>>(new CmsWorkflowService());

            //CmsWorkflowHistory
            container.Register<IEntityBusinessService<CmsWorkflowHistory, int>>(new CmsWorkflowHistoryService());

            //CmsEntityAnnotation
            container.Register<IEntityBusinessService<CmsEntityAnnotation, int>>(new CmsEntityAnnotationService());

            // CmsEntityMetaFieldLink
            container.Register<IEntityBusinessService<CmsEntityMetaFieldLink, int>>(new CmsEntityMetaFieldLinkService());
        }

        private static void RegisterEntityRevisionServices()
        {
            var container = DependencyServiceLocator.Current;

            container.Register<ICmsEntityRevisionService>(CmsConstants.PageTypeParameter, new CmsPageService());

        }

        private static void RegisterSearchServices()
        {
            CmsSearchServiceConfiguration.RegisterSearchArea<CmsPage>("Pages", 
                new CmsSearchService<CmsPage>(page => new CmsAdminSearchResult {
                    Id = page.Id,
                    TypeParameter = page.TypeParameter,
                    Title = page.PageName
                }));

            CmsSearchServiceConfiguration.RegisterSearchArea<CmsMediaContent>("Media",
                new CmsSearchService<CmsMediaContent>(content => new CmsAdminSearchResult
                {
                    Id = content.Id,
                    TypeParameter = "MediaContent",
                    Title = content.MediaContentName,
                }));

        }

        private static void RegisterValidationServices()
        {
            var container = DependencyServiceLocator.Current;

            container.Register<IEntityValidationService<CmsPage>>(new CmsPageValidationService());
            container.Register<IEntityValidationService<CmsEntityMeta>>(new CmsEntityMetaValidationService());
        }
    }
}
